import { Injectable } from '@nestjs/common';

@Injectable()
export class ApSyncService {
  static autoSync: number = 1;
  static sate: number = 1;
  static lonStore: number = Math.floor(Math.random() * 200);
  static status: number[] = [0, 1, 2, 3, 4, 5, 6, 7];
  private kaInitStateStatus: number[] = [0, 1, 2];
  private beam_index: number = 0;
  private bc_freq: number = 1575.42;
  async autoSync(autoSwitch: any) {
    ApSyncService.autoSync = autoSwitch;
    await new Promise((resolve) => setTimeout(resolve, 2000));
    return {
      code: 200,
      message: 'success',
    };
  }

  private setLonStore() {
    ApSyncService.lonStore = Math.floor(Math.random() * 200);
    return ApSyncService.lonStore;
  }

  async postLonStore(lon_store: any) {
    ApSyncService.lonStore = lon_store;
    await new Promise((resolve) => setTimeout(resolve, 2000));
    return {
      code: 200,
      message: 'success',
    };
  }
  async postAutoGet() {
    return {
      code: 200,
      message: 'success',
      data: {
        auto_network: [
          {
            cp_id: 0,
            cp_name: 'cp1',
            is_auto: 0,
          },
          {
            cp_id: 1,
            cp_name: 'cp2',
            is_auto: 0,
          },
        ],
      },
    };
  }
  async postLonStoreGet() {
    return {
      code: 200,
      message: 'success',
      data: {
        lon_store: ApSyncService.lonStore,
      },
    };
  }
  async postState() {
    return {
      code: 200,
      message: 'success',
      data: {
        state:
          ApSyncService.status[
            Math.floor(Math.random() * ApSyncService.status.length)
          ],
        ka_init_state:
          this.kaInitStateStatus[
            Math.floor(Math.random() * this.kaInitStateStatus.length)
          ],
      },
    };
  }

  async postGetStar() {
    return {
      code: 200,
      message: 'success',
      data: {
        sate: ApSyncService.sate,
      },
    };
  }

  async postSetStar(sate: any) {
    console.log(sate, 'sate');
    ApSyncService.sate = sate;
    return {
      code: 200,
      message: 'success',
    };
  }

  async postSetBeamIndex(beam: any) {
    this.beam_index = beam;
    return {
      code: 200,
      message: 'success',
    };
  }

  async postGetBeamIndex() {
    return {
      code: 200,
      message: 'success',
      data: { beam: this.beam_index },
    };
  }

  async postSetBcFreq(bc_freq: any) {
    this.bc_freq = bc_freq;
    return {
      code: 200,
      message: 'success',
    };
  }

  async postGetBcFreq() {
    return {
      code: 200,
      message: 'success',
      data: { bc_freq: this.bc_freq },
    };
  }

  private freqList = [
    {
      sat: 1,
      freq_info: [
        {
          id: 0,
          kd_name: 'KD101',
          downlink_freq: 18660000,
          ssb_position: 122,
          uplink_freq: 28740000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 18001920000,
          bandwidth: 50,
        },
        {
          id: 1,
          kd_name: 'KD102',
          downlink_freq: 18819840,
          ssb_position: 122,
          uplink_freq: 28900020,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
        {
          id: 2,
          kd_name: 'KD103',
          downlink_freq: 19020000,
          ssb_position: 122,
          uplink_freq: 29100000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 18001920000,
          bandwidth: 50,
        },
        {
          id: 3,
          kd_name: 'KD104',
          downlink_freq: 19380000,
          ssb_position: 122,
          uplink_freq: 29460000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
        {
          id: 4,
          kd_name: 'KD105',
          downlink_freq: 19539840,
          ssb_position: 122,
          uplink_freq: 29260020,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 18001920000,
          bandwidth: 50,
        },
        {
          id: 5,
          kd_name: 'KD106',
          downlink_freq: 19740000,
          ssb_position: 122,
          uplink_freq: 29820000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
      ],
    },
    {
      sat: 2,
      freq_info: [
        {
          id: 0,
          kd_name: 'KD201',
          downlink_freq: 18544800,
          ssb_position: 122,
          uplink_freq: 28785000,
          downlink_bdc: 5,
          uplink_buc: 3,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
        {
          id: 1,
          kd_name: 'KD202',
          downlink_freq: 18825600,
          ssb_position: 122,
          uplink_freq: 5,
          downlink_bdc: 5,
          uplink_buc: 3,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
        {
          id: 2,
          kd_name: 'KD203',
          downlink_freq: 18904800,
          ssb_position: 122,
          uplink_freq: 29025000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
        {
          id: 3,
          kd_name: 'KD204',
          downlink_freq: 19545600,
          ssb_position: 122,
          uplink_freq: 29145000,
          downlink_bdc: 5,
          uplink_buc: 3,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
        {
          id: 4,
          kd_name: 'KD205',
          downlink_freq: 19624800,
          ssb_position: 122,
          uplink_freq: 29265000,
          downlink_bdc: 5,
          uplink_buc: 3,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
        {
          id: 5,
          kd_name: 'KD206',
          downlink_freq: 19984800,
          ssb_position: 122,
          uplink_freq: 29385000,
          downlink_bdc: 5,
          uplink_buc: 3,
          sib1_downlink: 19539840000,
          bandwidth: 50,
        },
      ],
    },
    {
      sat: 3,
      freq_info: [
        {
          id: 0,
          kd_name: 'KD301',
          downlink_freq: 18300000,
          ssb_position: 122,
          uplink_freq: 28140000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 18300000,
          bandwidth: 100,
        },
        {
          id: 1,
          kd_name: 'KD302',
          downlink_freq: 18660000,
          ssb_position: 122,
          uplink_freq: 28260000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 18660000,
          bandwidth: 100,
        },
        {
          id: 2,
          kd_name: 'KD303',
          downlink_freq: 19020000,
          ssb_position: 122,
          uplink_freq: 28380000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 19020000,
          bandwidth: 100,
        },
        {
          id: 3,
          kd_name: 'KD304',
          downlink_freq: 18420000,
          ssb_position: 122,
          uplink_freq: 29460000,
          downlink_bdc: 0,
          uplink_buc: 0,
          sib1_downlink: 18420000,
          bandwidth: 100,
        },
      ],
    },
  ];

  async postGetFreq() {
    return {
      code: 200,
      message: 'success',
      data: {
        freq: this.freqList,
      },
    };
  }

  async postSetFreq(body: any) {
    const { sat, freq_info } = body;
    const freqItem = this.freqList.find((item) => item.sat === sat);
    if (freqItem) {
      freq_info.forEach((newItem) => {
        const existingItem = freqItem.freq_info.find(
          (item) => item.id === newItem.id,
        );
        if (existingItem) {
          Object.assign(existingItem, newItem);
        } else {
          freqItem.freq_info.push(newItem);
        }
      });
      return {
        code: 200,
        message: 'success',
      };
    }
    return {
      code: 400,
      message: '卫星未找到',
    };
  }

  async postDeletefreq(body: any) {
    const { sat, id } = body;
    const freqItem = this.freqList.find((item) => item.sat === sat);
    if (freqItem) {
      freqItem.freq_info = freqItem.freq_info.filter((item) => item.id !== id);
    }
    return {
      code: 200,
      message: 'success',
    };
  }
}
